import Vue from "vue"
import VueRouter from "vue-router"

import store from "@/store"
import MainSlot from "@/components/layout/MainSlot"

import Article from "./modules/article"
import FingerConfig from "./modules/fingerconfig"
import Coupon from "./modules/coupon"
import ManualReport from "./modules/manualreport"

Vue.use(VueRouter)

const routes = [
  {
    path: "/signin",
    name: "SignIn",
    component: () =>
      import(/* webpackChunkName: "sign" */ "../views/SignIn.vue"),
    meta: {
      requiresAuth: false,
      bodyClass: "hold-transition login-page",
    },
  },
  {
    path: "/login",
    name: "LogIn",
    component: () =>
      import(/* webpackChunkName: "sign" */ "../views/SignIn.vue"),
    meta: {
      requiresAuth: false,
      bodyClass: "hold-transition login-page",
    },
  },
  {
    path: "/",
    name: "Main",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: MainSlot,
    meta: {
      requiresAuth: true,
      bodyClass: "hold-transition skin-blue sidebar-mini",
    },
    children: [...Article, ...FingerConfig, ...Coupon, ...ManualReport],
  },
]

const router = new VueRouter({
  routes,
})

// 解决导航栏中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
  if (to.matched && to.matched.length > 0) {
    let token = store.getters["auth/token"] || null
    const authorized = token && token.checkValid()

    if (authorized) {
      // if (to.matched.some((record) => !record.meta.requiresAuth)) {
      //   next({ name: "Main" });
      // }
      next()
    } else {
      if (to.matched.some((record) => record.meta.requiresAuth)) {
        next({ name: "SignIn" })
      } else {
        next() // 不用登录的情况
      }
    }
  } else {
    next({ name: "PageNotFound" })
  }
})

const menus = [
  {
    label: "文章管理",
    icon: "access_time",
    children: [
      {
        label: "文章类别",
        icon: "mdi-run",
        route: "ArticleCategory",
        role: "admin",
      },
      {
        label: "文章发布",
        icon: "mdi-calendar-check",
        route: "Article",
        role: "admin",
      },
      {
        label: "文章列表",
        icon: "mdi-run",
        route: "ArticleList",
        role: "admin",
      },
    ],
  },
  {
    label: "报告模板",
    icon: "mdi-cog",
    children: [
      {
        label: "体验版配置",
        icon: "mdi-run",
        route: "SimpleFingerReportConfig",
        role: "admin",
      },
      {
        label: "学习风格",
        icon: "mdi-run",
        route: "LearnTraitConfig",
        role: "admin",
      },
      {
        label: "学习类型",
        icon: "mdi-run",
        route: "LearnTypeConfig",
        role: "admin",
      },
      {
        label: "自然属性",
        icon: "mdi-run",
        route: "NaturalPropertyConfig",
        role: "admin",
      },
      {
        label: "多元智能",
        icon: "mdi-run",
        route: "OmniIntelligenceConfig",
        role: "admin",
      },
      {
        label: "纹路解析",
        icon: "mdi-run",
        route: "ClassifyAnalyseConfig",
        role: "admin",
      },
    ],
  },
  {
    label: "商品管理",
    icon: "access_time",
    children: [
      {
        label: "商品分类",
        icon: "mdi-run",
        route: "CouponCategory",
        role: "admin",
      },
      {
        label: "商品列表",
        icon: "mdi-run",
        route: "CouponList",
        role: "admin",
      },
      {
        label: "轮播图",
        icon: "mdi-run",
        route: "LunboList",
        role: "admin",
      },
      {
        label: "爆款商品",
        icon: "mdi-run",
        route: "BaokuanList",
        role: "admin",
      },
    ],
  },
  {
    label: "手工报告",
    icon: "mdi-file-cabinet",
    children: [
      {
        label: "新增报告",
        icon: "mdi-open-in-new",
        route: "FingerReportManualEditor",
        role: "admin",
      },
      {
        label: "历史报告",
        icon: "mdi-format-list-checkbox",
        route: "FingerReportManualList",
        role: "admin",
      },
    ],
  },
]

export default router
export { router, menus }
